package com.cdtye.itps.cms.service.datasysn;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cdtye.common.kafkacore.model.attr.ConsumerErrorAttr;
import com.cdtye.common.kafkacore.model.entity.KfkConsumerRule;
import com.cdtye.common.kafkacore.model.enums.KfkErrorFieldTypeEnum;
import com.cdtye.common.kafkacore.model.enums.KfkErrorTypeEnum;
import com.cdtye.common.kafkacore.service.KfkConsumerRuleService;
import com.cdtye.common.webcore.constants.WebConstants;
import com.cdtye.common.webcore.enums.YesNoEnum;
import com.cdtye.common.webcore.util.ObjectUtil;
import com.cdtye.itps.cms.dao.defect.entity.JcwProblemDefectInfo;
import com.cdtye.itps.cms.dao.dept.entity.SysDept;
import com.cdtye.itps.cms.dao.defect.entity.JcwDefectInfoPhoto;
import com.cdtye.itps.cms.dao.detect.JcjcDefectInfoSection;
import com.cdtye.itps.cms.dao.detect.JcjcDefectSourceTypeSection;
import com.cdtye.itps.cms.dao.enums.SysRelationMapping;
import com.cdtye.itps.cms.dao.direction.entity.JcDirection;
import com.cdtye.itps.cms.dao.lineSite.entity.JcLineSite;
import com.cdtye.itps.cms.dao.line.entity.DelectJcSecline;
import com.cdtye.itps.cms.mapper.defect.JcjcDefectInfoSectionMapper;
import com.cdtye.itps.cms.mapper.defect.JcwProblemDefectInfoMapper;
import com.cdtye.itps.cms.service.defect.JcjcDefectSourceTypeService;
import com.cdtye.itps.cms.service.defect.JcwDefectInfoPhotoService;
import com.cdtye.itps.cms.service.defect.SysRelationMappingService;
import com.cdtye.itps.cms.service.dept.SysDeptService;
import com.cdtye.itps.cms.service.direction.JcDirectionService;
import com.cdtye.itps.cms.service.line.DefectJcSeclineService;
import com.cdtye.itps.cms.service.lineSite.JcLineSiteService;
import com.cdtye.itps.models.base.model.attr.basic.*;
import com.cdtye.itps.models.base.model.attr.defect.DefectGradeAttr;
import com.cdtye.itps.models.base.model.attr.defect.UserDefectTypeAttr;
import com.cdtye.itps.models.base.model.attr.sysm.DeptAttr;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class JcjcHandleService {

    private static final String LINE_END_X = "线";
    private static final String SITE_END_Z = "站";
    private static final String PILLAR_PT = "普铁";
    private static final String PILLAR_GT = "高铁";
    private static final String PILLAR_ZZ = "支柱";

    @Autowired
    private KfkConsumerRuleService kfkConsumerRuleService;
    @Autowired
    private JcjcDefectSourceTypeService jcjcDefectSourceTypeService;
    @Autowired
    private SysRelationMappingService sysRelationMappingService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private JcDirectionService jcDirectionService;
    @Autowired
    private DefectJcSeclineService JcSeclineService;
    @Autowired
    private JcLineSiteService JcLineSiteService;
    @Autowired
    private JcwProblemDefectInfoMapper jcwProblemDefectInfoMapper;
    @Autowired
    private JcjcDefectInfoSectionMapper jcjcDefectInfoSectionMapper;
    @Autowired
    private JcwDefectInfoPhotoService jcwDefectInfoPhotoService;


    public Object checkDeptInfo(DeptAttr deptAttr) {
        String deptCode = deptAttr == null ? "" : deptAttr.getDeptCode();
        if (StringUtils.isBlank(deptCode)) {
            deptCode = deptAttr.getDeptName();
            if (StringUtils.isBlank(deptCode)) {
                return getDeptErrorAttr(deptCode, "接口传递部门编码为空");
            }
        }

        //1、根据code获取数据
        List<SysDept> entity = null;

        QueryWrapper<SysDept> DeptWrapper = new QueryWrapper<>();
        DeptWrapper.eq("DEPT_NAME", deptAttr.getDeptName());
        entity = sysDeptService.list(DeptWrapper);
        if (entity.size() == 0){
            QueryWrapper<SysRelationMapping> queryRelationMappingWrapper = new QueryWrapper<>();
            queryRelationMappingWrapper.eq("TYPE","dept");
            queryRelationMappingWrapper.eq("VALUE", deptCode);
            SysRelationMapping sysRelationMapping = sysRelationMappingService.getOne(queryRelationMappingWrapper);
            if(sysRelationMapping == null){
                return getDeptErrorAttr(deptCode, "映射表无对应部门数据");
            }
            QueryWrapper<SysDept> queryDeptWrapper = new QueryWrapper<>();
            queryDeptWrapper.eq("DEPT_ID", sysRelationMapping.getKey());
            entity = sysDeptService.list(queryDeptWrapper);
        }


        if (entity.size() == 0) {
            //防止预处理无错误id
            if (StringUtils.isBlank(deptCode)) {
                deptCode = deptAttr.getDeptName();
            }
            return getDeptErrorAttr(deptCode, "部门不存在");
        }
        return entity.get(0);
    }
    /**
     * 获取部门错误默认属性
     *
     * @param errorCode
     * @param errorReason
     * @return
     */
    public ConsumerErrorAttr getDeptErrorAttr(String errorCode, String errorReason) {
        return new ConsumerErrorAttr().setErrorType(KfkErrorTypeEnum.DEPT.getType()).setErrorCode(errorCode).setErrorReason(errorReason)
                .setErrorField("deptAttr").setErrorFieldType(KfkErrorFieldTypeEnum.OBJECT.getType())
                .setErrorClazzName(DeptAttr.class.getName()).setStatus(YesNoEnum.NO.getCode());
    }

    /**
     * 验证线路
     *
     * @param allLineMap
     * @param lineAttr
     * @return 返回若是ConsumerErrorAttr对象时，表示预处理异常。
     * 预处理异常对象会设置默认的字段名为errorField=xxAttr,errorFieldType=Object,errorClazz=对象.class
     * 这几个值需要根据业务判断是否修改。
     */
    public Object checkLine(Map<String, DelectJcSecline> allLineMap, LineAttr lineAttr, String dataSysnKey) {
        String lineCode = lineAttr == null ? "" : lineAttr.getLineCode(), lineName = lineAttr == null ? "" : lineAttr.getLineName();
        if (StringUtils.isBlank(lineCode) || StringUtils.isBlank(lineName)) {
            return getLineErrorAttr(lineCode, "该记录未挂接线路");
        }
        //1、根据code获取数据
        DelectJcSecline entity = allLineMap.get(lineCode);
        //2、根据name获取数据
        if (entity == null && StringUtils.isNotBlank(lineName)) {
            entity = allLineMap.get(lineName);
            //3、匹配去掉"线"
            if (entity == null && lineName.endsWith(LINE_END_X)) {
                entity = allLineMap.get(lineName.substring(0, lineName.lastIndexOf(LINE_END_X)));
            }
        }
        //4、按规则预处理
        String errorType = KfkErrorTypeEnum.LINE.getType();
        if (entity == null) {
            List<KfkConsumerRule> ruleList = kfkConsumerRuleService.findByErrorType(errorType, dataSysnKey);
            for (KfkConsumerRule o : ruleList) {
                //防止没有该字段报错
                if (o.getType() == null) {
                    continue;
                }
                if (o.getType() == 0) {
                    //编码预处理,按编码或名称
                    String key = o.getErrorType() + o.getErrorCode();
                    //按异常编码进行匹配校正
                    boolean codeFlag = StringUtils.isNotBlank(lineCode) && key.equals(errorType + lineCode);
                    boolean nameFlag = StringUtils.isNotBlank(lineName) && key.equals(errorType + lineName);
                    if (codeFlag || nameFlag) {
                        entity = allLineMap.get(JSON.parseObject(JSON.toJSONString(o.getData()), DelectJcSecline.class).getLineId());
                        break;
                    }
                } else if (o.getType() == 1 && StringUtils.isNotBlank(lineName)) {
                    //别名预处理，按名称
                    String newName = "";
                    for (String rule : o.getRuleList()) {
                        if (lineName.equals(rule)) {
                            newName = JSON.parseObject(JSON.toJSONString(o.getData()), LineAttr.class).getLineCode();
                            break;
                        }
                    }
                    if (StringUtils.isNotBlank(newName)) {
                        entity = allLineMap.get(newName);
                        break;
                    }
                } else if (o.getType() == 2 && StringUtils.isNotBlank(lineName)) {
                    //替换预处理,按名称
                    String newName = "";
                    for (String rule : o.getRuleList()) {
                        if (lineName.endsWith(rule)) {
                            newName = lineName.substring(0, lineName.lastIndexOf(rule)) + ObjectUtil.covToString(o.getData()).replaceAll("\\s*", "");
                            break;
                        }
                    }
                    if (StringUtils.isNotBlank(newName)) {
                        entity = allLineMap.get(newName);
                        break;
                    }
                }
            }
        }
        if (entity == null) {
            //防止预处理无错误code
            if (StringUtils.isBlank(lineCode)) {
                lineCode = lineName;
            }
            return getLineErrorAttr(lineCode, "线路不存在");
        }
        return entity;
    }

    /**
     * 获取线路错误默认属性
     *
     * @param errorCode
     * @param errorReason
     * @return
     */
    public ConsumerErrorAttr getLineErrorAttr(String errorCode, String errorReason) {
        return new ConsumerErrorAttr().setErrorType(KfkErrorTypeEnum.LINE.getType()).setErrorCode(errorCode).setErrorReason(errorReason)
                .setErrorField("lineAttr").setErrorFieldType(KfkErrorFieldTypeEnum.OBJECT.getType())
                .setErrorClazzName(LineAttr.class.getName()).setStatus(YesNoEnum.NO.getCode());
    }

    /**
     * 匹配区站
     *
     * @param allSiteMap
     * @param siteAttr
     * @return 返回若是ConsumerErrorAttr对象时，表示预处理异常。
     * 预处理异常对象会设置默认的字段名为errorField=xxAttr,errorFieldType=Object,errorClazz=对象.class
     * 这几个值需要根据业务判断是否修改。
     */
    public Object checkSite(Map<String, JcLineSite> allSiteMap, SiteAttr siteAttr, String lineName, String lineId, String dataSysnKey) {
        String siteCode = siteAttr.getLineSiteCode(), siteName = siteAttr.getSiteName(), siteId = siteAttr.getSiteId();
        if (StringUtils.isBlank(siteCode) && StringUtils.isBlank(siteName)) {
            return getSiteErrorAttr(siteCode, "该记录未挂接区站");
        }
        //1、根据code获取数据
        JcLineSite entity = allSiteMap.get(siteCode);
        //特殊处理，根据Id取
        if (entity == null) {
            entity = allSiteMap.get(siteId);
        }
        //2、根据name获取数据
        if (entity == null && StringUtils.isNotBlank(siteName)) {
            entity = allSiteMap.get(lineId + siteName);
            //3、匹配去掉"站"
            if (entity == null) {
                entity = allSiteMap.get(lineId + siteName.replaceAll(SITE_END_Z, ""));
            }
            //4、匹配站场交换
            if (entity == null && siteName.contains(WebConstants.CHAR_HX)) {
                String[] siteArr = siteName.split(WebConstants.CHAR_HX);
                String tempSiteName = siteArr[1] + WebConstants.CHAR_HX + siteArr[0];
                entity = allSiteMap.get(lineId + tempSiteName);
                //匹配交换并去掉"站"
                if (entity == null) {
                    entity = allSiteMap.get(lineId + tempSiteName.replaceAll(SITE_END_Z, ""));
                }
            }
        }
        //4、按规则预处理
        String errorType = KfkErrorTypeEnum.LINE_SITE.getType();
        //线路名称_区站名称
        lineName = lineName == null ? "线路名称为空" : lineName;
        String errorCode = StringUtils.isBlank(siteName) ? "" : new StringBuilder(lineName).append("_").append(siteName).toString();
        if (entity == null) {
            List<KfkConsumerRule> ruleList = kfkConsumerRuleService.findByErrorType(errorType, dataSysnKey);
            for (KfkConsumerRule o : ruleList) {
                //防止没有该字段报错
                if (o.getType() == null) {
                    continue;
                }
                if (o.getType() == 0) {
                    //编码预处理,按编码或名称
                    String key = o.getErrorType() + o.getErrorCode();
                    //按异常编码进行匹配校正
                    boolean codeFlag = StringUtils.isNotBlank(siteCode) && key.equals(errorType + siteCode);
                    boolean nameFlag = StringUtils.isNotBlank(errorCode) && key.equals(errorType + errorCode);
                    if (codeFlag || nameFlag) {
                        entity = allSiteMap.get(JSON.parseObject(JSON.toJSONString(o.getData()), JcLineSite.class).getSiteId());
                        break;
                    }
                } else if (o.getType() == 1 && StringUtils.isNotBlank(siteName)) {
                    //别名预处理，按名称
                    String newName = "";
                    for (String rule : o.getRuleList()) {
                        if (siteName.equals(rule)) {
                            newName = JSON.parseObject(JSON.toJSONString(o.getData()), SiteAttr.class).getSiteName();
                            break;
                        }
                    }
                    if (StringUtils.isNotBlank(newName)) {
                        entity = allSiteMap.get(lineId + newName);
                        break;
                    }
                } else if (o.getType() == 2 && StringUtils.isNotBlank(siteName)) {
                    //替换预处理,按名称
                    String newName = "";
                    for (String rule : o.getRuleList()) {
                        if (siteName.endsWith(rule)) {
                            newName = siteName.substring(0, siteName.lastIndexOf(rule)) + ObjectUtil.covToString(o.getData()).replaceAll("\\s*", "");
                            break;
                        }
                    }
                    if (StringUtils.isNotBlank(newName)) {
                        entity = allSiteMap.get(lineId + newName);
                        break;
                    }
                }
            }
        }
        if (entity == null) {
            //防止预处理无错误code
            if (StringUtils.isBlank(siteCode)) {
                siteCode = errorCode;
            }
            return getSiteErrorAttr(siteCode, "区站不存在");
        }
        return entity;
    }

    /**
     * 获取区站错误默认属性
     *
     * @param errorCode
     * @param errorReason
     * @return
     */
    public ConsumerErrorAttr getSiteErrorAttr(String errorCode, String errorReason) {
        return new ConsumerErrorAttr().setErrorType(KfkErrorTypeEnum.LINE_SITE.getType()).setErrorCode(errorCode).setErrorReason(errorReason)
                .setErrorField("siteAttr").setErrorFieldType(KfkErrorFieldTypeEnum.OBJECT.getType())
                .setErrorClazzName(SiteAttr.class.getName()).setStatus(YesNoEnum.NO.getCode());
    }

    /**
     * 匹配行别
     *
     * @param allDirectionMap
     * @param directionAttr
     * @return 返回若是ConsumerErrorAttr对象时，表示预处理异常。
     * 预处理异常对象会设置默认的字段名为errorField=xxAttr,errorFieldType=Object,errorClazz=对象.class
     * 这几个值需要根据业务判断是否修改。
     */
    public Object checkDirection(Map<String, JcDirection> allDirectionMap, DirectionAttr directionAttr,String dataSysnKey) {
        String directionCode = directionAttr == null ? "" : directionAttr.getDirectionCode(), directionName = directionAttr == null ? "" : directionAttr.getDirectionName();
        if (StringUtils.isBlank(directionCode) && StringUtils.isBlank(directionName)) {
            return new JcDirection();
        }
        //1、根据code获取数据
        JcDirection entity = allDirectionMap.get(directionCode);
        //2、根据name获取数据
        if (entity == null) {
            entity = allDirectionMap.get(directionName);
        }
        //3、按规则预处理
        String errorType = KfkErrorTypeEnum.DIRECTION.getType();
        String errorCode = directionAttr.getDirectionCode();
        if (entity == null) {
            List<KfkConsumerRule> ruleList = kfkConsumerRuleService.findByErrorType(errorType, dataSysnKey);
            for (KfkConsumerRule o : ruleList) {
                //防止没有该字段报错
                if (o.getType() == null) {
                    continue;
                }
                if (o.getType() == 0) {
                    //编码预处理,按编码或名称
                    String key = o.getErrorType() + o.getErrorCode();
                    //按异常编码进行匹配校正
                    boolean codeFlag = StringUtils.isNotBlank(directionCode) && key.equals(errorType + directionCode);
                    boolean nameFlag = StringUtils.isNotBlank(errorCode) && key.equals(errorType + errorCode);
                    if (codeFlag || nameFlag) {
                        entity = allDirectionMap.get(JSON.parseObject(JSON.toJSONString(o.getData()), DirectionAttr.class).getDirectionId());
                        break;
                    }
                } else if (o.getType() == 1 && StringUtils.isNotBlank(directionName)) {
                    //别名预处理，按名称
                    String newName = "";
                    for (String rule : o.getRuleList()) {
                        if (directionName.equals(rule)) {
                            newName = JSON.parseObject(JSON.toJSONString(o.getData()), DirectionAttr.class).getDirectionCode();
                            break;
                        }
                    }
                    if (StringUtils.isNotBlank(newName)) {
                        entity = allDirectionMap.get(newName);
                        break;
                    }
                } else if (o.getType() == 2 && StringUtils.isNotBlank(directionName)) {
                    //替换预处理,按名称
                    String newName = "";
                    for (String rule : o.getRuleList()) {
                        if (directionName.endsWith(rule)) {
                            newName = directionName.substring(0, directionName.lastIndexOf(rule)) + ObjectUtil.covToString(o.getData()).replaceAll("\\s*", "");
                            break;
                        }
                    }
                    if (StringUtils.isNotBlank(newName)) {
                        entity = allDirectionMap.get(newName);
                        break;
                    }
                }
            }
        }
        if (entity == null) {
            //防止预处理无错误code
            if (StringUtils.isBlank(directionCode)) {
                directionCode = directionName;
            }
            return getDirectionErrorAttr(directionCode, "行别不存在");
        }
        return entity;
    }

    /**
     * 获取行别错误默认属性
     *
     * @param errorCode
     * @param errorReason
     * @return
     */
    public ConsumerErrorAttr getDirectionErrorAttr(String errorCode, String errorReason) {
        return new ConsumerErrorAttr().setErrorType(KfkErrorTypeEnum.DIRECTION.getType()).setErrorCode(errorCode).setErrorReason(errorReason)
                .setErrorField("directionAttr").setErrorFieldType(KfkErrorFieldTypeEnum.OBJECT.getType())
                .setErrorClazzName(DirectionAttr.class.getName()).setStatus(YesNoEnum.NO.getCode());
    }

    /**
     * 获取支柱错误默认属性
     *
     * @param errorCode
     * @param errorReason
     * @return
     */
    public ConsumerErrorAttr getPillarErrorAttr(String errorCode, String errorReason) {
        return new ConsumerErrorAttr().setErrorType(KfkErrorTypeEnum.PILLAR.getType()).setErrorCode(errorCode).setErrorReason(errorReason)
                .setErrorField("pillarAttr").setErrorFieldType(KfkErrorFieldTypeEnum.OBJECT.getType())
                .setErrorClazzName(PillarAttr.class.getName()).setStatus(YesNoEnum.NO.getCode());
    }



    /**
     * 获取用户缺陷分类错误默认属性
     *
     * @param errorCode
     * @param errorReason
     * @return
     */
    public ConsumerErrorAttr getUserDefectTypeErrorAttr(String errorCode, String errorReason) {
        return new ConsumerErrorAttr().setErrorType(KfkErrorTypeEnum.DEFECT_TYPE.getType()).setErrorCode(errorCode).setErrorReason(errorReason)
                .setErrorField("userDefectTypeAttr").setErrorFieldType(KfkErrorFieldTypeEnum.OBJECT.getType())
                .setErrorClazzName(UserDefectTypeAttr.class.getName()).setStatus(YesNoEnum.NO.getCode());
    }

    /**
     * 查询所有行别数据，用于验证
     *
     * @return key:行别名称,value:行别数据
     */
    public Map<String, JcDirection> findDirectionsMap(String key) {
        List<JcDirection> datas = new ArrayList<>();

        QueryWrapper<JcDirection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        datas = jcDirectionService.list(queryWrapper);
        //注 行别表没有code
        Map<String, JcDirection> map = datas.stream().collect(Collectors.toMap(JcDirection::getDirectionId, o -> o, (k1, k2) -> k1));
        map.putAll(datas.stream().collect(Collectors.toMap(JcDirection::getDirectionName, o -> o, (k1, k2) -> k1)));
        return map;
    }


    /**
     * 查询所有部门数据，用于验证
     *
     * @return key:部门编码 部门名称,value:部门数据
     */
    public Map<String, SysDept> findDeptsMap(String key) {
        List<SysDept> datas = new ArrayList<>();

        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        //queryWrapper.ne("del_flag","Y");
        datas = sysDeptService.list(queryWrapper);
        //添加key为部门编码数据
        Map<String, SysDept> map = datas.stream().filter(o -> StringUtils.isNotBlank(o.getDeptId()))
                .collect(Collectors.toMap(SysDept::getDeptId, o -> o, (k1, k2) -> k1));
        //添加key为部门名称数据
        map.putAll(datas.stream().filter(o -> StringUtils.isNotBlank(o.getDeptName()))
                .collect(Collectors.toMap(SysDept::getDeptName, o -> o, (k1, k2) -> k1)));
        map.putAll(datas.stream().filter(o -> StringUtils.isNotBlank(o.getDeptNo()))
                .collect(Collectors.toMap(SysDept::getDeptNo, o -> o, (k1, k2) -> k1)));
        return map;
    }

    /**
     * 查询所有线路数据，用于验证*
     * @return key:线路编码 线路名称,value:线路数据
     */
    public Map<String, DelectJcSecline> findLinesMap(String key) {
        List<DelectJcSecline> datas = new ArrayList<>();
        QueryWrapper<DelectJcSecline> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        datas = JcSeclineService.list(queryWrapper);
        Map<String, DelectJcSecline> map = datas.stream().filter(o -> StringUtils.isNotBlank(o.getLineId()))
                .collect(Collectors.toMap(DelectJcSecline::getLineId, o -> o, (k1, k2) -> k1));
        map.putAll(datas.stream().filter(o -> StringUtils.isNotBlank(o.getSeclineName()))
                .collect(Collectors.toMap(DelectJcSecline::getSeclineName, o -> o, (k1, k2) -> k1)));
        return map;
    }

    /**
     * 查询接触网缺陷来源
     *
     */
    public JcjcDefectSourceTypeSection findDefectSourceTypeId(String key) {
        JcjcDefectSourceTypeSection jcjcDefectSourceTypeSection = null;
        QueryWrapper<JcjcDefectSourceTypeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.eq("source_type_name", "6C");
        jcjcDefectSourceTypeSection = jcjcDefectSourceTypeService.getOne(queryWrapper);
        return jcjcDefectSourceTypeSection;
    }

    /**
     * 查询所有线路区站数据，用于验证
     *
     * @return key:线路区站编码 线路区站名称,value:线路区站数据
     */
    public Map<String, JcLineSite> findLineSitesMap(String key) {
        List<JcLineSite> datas = new ArrayList<>();

        QueryWrapper<JcLineSite> queryWrapper = new QueryWrapper<>();
        //queryWrapper.ne("del_flag","Y");
        datas = JcLineSiteService.list(queryWrapper);
        //未做完继续JC_LINE_SITE表缺少线路区站code
        Map<String, JcLineSite> map = datas.stream().filter(o -> StringUtils.isNotBlank(o.getSiteId()))
                .collect(Collectors.toMap(JcLineSite::getSiteName, o -> o, (k1, k2) -> k1));
        map.putAll(getLineSiteName(datas));

        return map;
    }

    /**
     * 封装线路区站名称key   线路ID+区站名称
     *
     * @param datas
     * @return
     */
    private Map<String, JcLineSite> getLineSiteName(List<JcLineSite> datas) {
        Map<String, JcLineSite> map = new HashMap<>(16);
        datas.forEach(o -> {
            map.put(o.getSeclineId() + o.getSiteName(), o);
        });
        return map;
    }


    /**
     * 查询局级缺陷  by id 或 支柱id+发现日期+缺陷类型+缺陷等级
     * @param jdis
     * @return
     */
    public List<JcwProblemDefectInfo> selectIdBureauProblemDefectInfo(JcwProblemDefectInfo jdis){
        QueryWrapper<JcwProblemDefectInfo> queryWrapper = new QueryWrapper<>();
        List<JcwProblemDefectInfo> list = null;
        if(jdis.getDiscoverSourceId() != null){
            queryWrapper.ne("del_flag","Y");
            queryWrapper.eq("discover_source_id", jdis.getDiscoverSourceId());
            list = jcwProblemDefectInfoMapper.selectList(queryWrapper);
        }
        if (CollectionUtils.isEmpty(list)) {
            if (StringUtils.isNotBlank(jdis.getPillarId()) && null != jdis.getFindDate() && StringUtils.isNotBlank(jdis.getDefectCategoryId())) {
                queryWrapper = new QueryWrapper<>();
                queryWrapper.ne("del_flag","Y");
                queryWrapper.eq("pillar_id",jdis.getPillarId());
                queryWrapper.eq("find_date",jdis.getFindDate());
                queryWrapper.eq("defect_category_id",jdis.getDefectCategoryId());
                if(jdis.getCreateDatetimeSource() != null){
                    queryWrapper.eq("create_datetime_source",jdis.getCreateDatetimeSource());
                }
                if (StringUtils.isNotBlank(jdis.getDefectLevelId())) {
                    queryWrapper.eq("defect_level_id",jdis.getDefectLevelId());
                }
                list = jcwProblemDefectInfoMapper.selectList(queryWrapper);
            }
        }
        return list;
    }


    /**
     * 查询段级缺陷  by id 或 支柱id+发现日期+缺陷类型+缺陷等级
     * @param jdis
     * @return
     */
    public List<JcjcDefectInfoSection> selectIdSectionProblemDefectInfo(JcjcDefectInfoSection jdis){
        QueryWrapper<JcjcDefectInfoSection> queryWrapper = new QueryWrapper<>();
        List<JcjcDefectInfoSection> list = null;
        if(jdis.getDiscoverSourceId() != null){
            queryWrapper.ne("del_flag","Y");
            queryWrapper.eq("discover_source_id", jdis.getDiscoverSourceId());
            list = jcjcDefectInfoSectionMapper.selectList(queryWrapper);
        }
        if (CollectionUtils.isEmpty(list)) {
            if (StringUtils.isNotBlank(jdis.getPillarId()) && null != jdis.getDiscoverDate() && StringUtils.isNotBlank(jdis.getDefectTypeId())) {
                queryWrapper = new QueryWrapper<>();
                queryWrapper.ne("del_flag","Y");
                queryWrapper.eq("pillar_id",jdis.getPillarId());
                queryWrapper.eq("Discover_Date",jdis.getDiscoverDate());
                queryWrapper.eq("Defect_Type_Id",jdis.getDefectTypeId());
                if(jdis.getCreateDatetime() != null){
                    queryWrapper.eq("Create_Datetime",jdis.getCreateDatetime());
                }
                if (StringUtils.isNotBlank(jdis.getDefectGradeId())) {
                    queryWrapper.eq("defect_Grade_Id",jdis.getDefectGradeId());
                }
                list = jcjcDefectInfoSectionMapper.selectList(queryWrapper);
            }
        }
        return list;
    }
    /**
     * 保存局缺陷信息
     *
     */
    public int insertBureauProblemDefectInfo(JcwProblemDefectInfo jcwProblemDefectInfo){
        return jcwProblemDefectInfoMapper.insert(jcwProblemDefectInfo);
    }

    /**
     * 保存段缺陷信息
     *
     */
    public int insertSectionProblemDefectInfo(JcjcDefectInfoSection jcjcDefectInfoSection){
        return jcjcDefectInfoSectionMapper.insert(jcjcDefectInfoSection);
    }

    /**
     * 保存局缺陷普片
     *
     */
    public boolean insertDefectInfoPhotoBureau(List<JcwDefectInfoPhoto> jdipList){
        return jcwDefectInfoPhotoService.insertDefectInfoPhoto(jdipList);
    }

    /**
     * 修改局缺陷信息
     *
     */
    public void updateBureauProblemDefectInfo(JcwProblemDefectInfo jcwProblemDefectInfo){
        UpdateWrapper<JcwProblemDefectInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",jcwProblemDefectInfo.getId());
        jcwProblemDefectInfoMapper.update(jcwProblemDefectInfo,updateWrapper);
    }

    /**
     * 修改局缺陷信息
     *
     */
    public void updateSectionProblemDefectInfo(JcjcDefectInfoSection jcjcDefectInfoSection){
        UpdateWrapper<JcjcDefectInfoSection> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("DEFECT_INFO_ID",jcjcDefectInfoSection.getDefectInfoId());
        jcjcDefectInfoSectionMapper.update(jcjcDefectInfoSection,updateWrapper);
    }

    /**
     * 获取用户缺陷等级错误默认属性
     *
     * @param errorCode
     * @param errorReason
     * @return
     */
    public ConsumerErrorAttr getUserDefectGradeErrorAttr(String errorCode, String errorReason) {
        return new ConsumerErrorAttr().setErrorType(KfkErrorTypeEnum.DEFECT_GRADE.getType()).setErrorCode(errorCode).setErrorReason(errorReason)
                .setErrorField("defectGradeAttr").setErrorFieldType(KfkErrorFieldTypeEnum.OBJECT.getType())
                .setErrorClazzName(DefectGradeAttr.class.getName()).setStatus(YesNoEnum.NO.getCode());
    }
}
